﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Acms.AcmsControls.ContentControls.FrontEnd;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class FloatingImageAdminDisplay : BaseAdminDisplay<FloatingImageContentControlProperties>
    {
        protected override string GetCssClass
        {
            get { return "image"; }
        }

        public override void PropertiesLoaded(FloatingImageContentControlProperties properties)
        {
            if (properties != null)
            {
                image.FileId = properties.ResourceId;
                altText = properties.AltText;
                width = properties.Width;
                height = properties.Height;
                Float = properties.Float;
            }
            this.Controls.Clear();
            CreateChildControls();
            EnsureChildControls();
        }


        private string altText;
        private int width;
        private int height;
        private FloatingImageContentControlProperties.FloatOptions Float;

        protected override object SaveViewState()
        {
            ViewState["AltText"] = altText;
            ViewState["Width"] = width;
            ViewState["Height"] = height;
            ViewState["Float"] = (int)Float;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            altText = ViewState["AltText"] as string;
            if (ViewState["Width"] is int)
            {
                width = (int)ViewState["Width"];
            }
            if (ViewState["Height"] is int)
            {
                height = (int)ViewState["Height"];
            }
            if (ViewState["Float"] is int)
            {
                Float = (FloatingImageContentControlProperties.FloatOptions)(int)ViewState["Float"];
            }
        }

        AcmsImage image = new AcmsImage();
        protected override void CreateChildControls()
        {
            image.RenderWidth = 250;
            image.RenderHeight = 250;
            this.Controls.Add(image);
            base.CreateChildControls();
        }


        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (image.FileId > 0)
            {
                image.RenderControl(writer);

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, "Float"));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Float:");
                writer.RenderEndTag();//label

                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "Float"));
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(Float.ToString());
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, "AltText"));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Alternate Text:");
                writer.RenderEndTag();//label

                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "AltText"));
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(altText);
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, string.Concat(this.ClientID, "Dimensions"));
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Dimensions:");
                writer.RenderEndTag();//lable

                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "Dimensions"));
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write("{0}px x {1}px", width, height);
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li

                writer.RenderEndTag();//ul
            }
            else
            {
                writer.Write("No image has been uploaded");
            }
        }
    }
}
